<?php
namespace app\admin\controller;

use app\admin\model\ActionLog as ActionLogModel;
use app\admin\model\Menu as MenuModel;

/**
 * 行为管理控制器
 * @package app\admin\controller
 */
class Actionlog extends Base
{
    /**
     * 首页
     * @author @author 唐小虎 <1731050423@qq.com>
     * @return mailparse_determine_best_xfer_encoding(fp)
     */
    public function index()
    {

        // 数据列表
        $data_list = ActionLogModel::order('id desc')->paginate();
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // print_r($data_list);die;
        $this->assign('meta_title', '行为管理');
        $this->assign('list', $data_list);
        return $this->fetch();
    }

    public function edit($id = '0')
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Action');
            // 验证失败 输出错误信息
            if (true !== $result) {
                $this->error($result);
            }

            if ($id > 0) {
                $action = ActionModel::update($data);
            } else {
                $action = ActionModel::create($data);
            }
            if ($action) {
                // Cache::clear();
                // 记录行为
                action_log('update_action', 'action', $action['id'], UID);
                $this->success('操作成功', cookie('__forward__'));
            } else {
                $this->error('操作失败', cookie('__forward__'));
            }
        }
        if ($id > 0) {
            $this->assign('meta_title', '编辑行为');
        } else {
            $this->assign('meta_title', '新增行为');
        }
        $map['status'] = 1;
        $map['pid']    = 0;
        $modules       = MenuModel::where($map)->order('sort,id')->column('icon,title,controller', 'id');
        $this->assign('modules', $modules);
        $info = ActionModel::get($id);
        $this->assign('info', $info);

        return $this->fetch();
    }
}
